package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test6 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test6.class.getResource(".").toURI());
        String packageName = Test6.class.getPackage().getName();
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File sub : subs){
            String fileName = sub.getName();
            String className = fileName.substring(0, fileName.indexOf("."));

            Class cls = Class.forName(packageName+"."+className);
            if(cls.isAnnotationPresent(AutoRunClass.class)){

                Object obj = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods){
                    if(method.isAnnotationPresent(AutoRunMethod.class)){
                        AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                        int value = arm.value();
                        System.out.println("开始调用"+method.getName()+value+"次");
                        for(int i=0;i<value;i++){
                            method.invoke(obj);
                        }
                    }
                }
            }
        }
    }
}
